package shuashuami.hb.com.http;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.adapter.TaskAdatper;
import shuashuami.hb.com.avtivity.CHallInfoActivity;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.share.ShareCUserInfo;

/**
 * Created by Administrator on 2017/7/10.
 */

public class CRecieveTaskHttp {

    private Activity activity;
    private String taskId;
    private TaskAdatper taskAdatper;
    private int position;

    private String num;

    public CRecieveTaskHttp(Activity activity, String taskId, TaskAdatper taskAdatper, int position, String num) {
        this.activity = activity;
        this.taskId = taskId;
        this.taskAdatper = taskAdatper;
        this.position = position;
        this.num = num;
    }

    public void recieveTask() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);
            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(activity);

        HttpCMethods.recieveTask(listener, shareCUserInfo.getId(), taskId);
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(activity);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            ToastUtil.showShort(activity, jsonObject.getString("message"));
                            taskAdatper.getList().remove(position);
                            taskAdatper.notifyDataSetChanged();
                            Intent intent = null;
                            intent = new Intent(activity, CHallInfoActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putString("id", taskId);
                            bundle.putString("num", num);
                            intent.putExtras(bundle);
                            activity.startActivity(intent);
                        } else {
                            ToastUtil.showShort(activity, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;

            }

            super.handleMessage(msg);
        }
    };
}
